package com.wteam.modules.system.util;


import com.wteam.modules.system.config.RabitMqConfig;
import com.wteam.modules.system.domain.SelectCourseRecord;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

@Component
@RequiredArgsConstructor
public class MqUtil {

    private final RabbitTemplate rabbitTemplate;


    @Value("${spring.rabbitmq.exchangeName2}")
    private String exchangeName;
    @Value("${spring.rabbitmq.routeKey.selectCourseKey1}")
    private String selectCourseRouteKey1;
    @Value("${spring.rabbitmq.routeKey.selectCourseKey2}")
    private String selectCourseRouteKey2;

    public String sendToSelectCourseQueue1(SelectCourseRecord selectCourseRecord) {
//        for (Long i = 0L; i < 10L; i++) {
//            SelectCourseRecord selectCourseRecord1 = new SelectCourseRecord();
//            selectCourseRecord1.setId(i);
//            // rabbitTemplate.convertSendAndReceive("exchange_fanout", "", "测试发布订阅模型：" + i);
//            rabbitTemplate.convertSendAndReceive(exchangeName, selectCourseRouteKey1, selectCourseRecord1);
//        }
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(exchangeName, selectCourseRouteKey1, selectCourseRecord,correlationData);
        String msg = null;
        while (RabitMqConfig.getResult(correlationData.getId()) == null){

        }

        msg = RabitMqConfig.getResult(correlationData.getId()) ;
        RabitMqConfig.remove(correlationData.getId());
        return msg;
    }

    public void sendToSelectCourseQueue2(SelectCourseRecord selectCourseRecord) {
        rabbitTemplate.convertAndSend(exchangeName, selectCourseRouteKey2, selectCourseRecord);
    }








}
